/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.server.shuffling;

import java.util.Comparator;

/**
 *
 * @author Czarek
 */
public abstract class ShufflingComparator<T extends IShufflable> implements Comparator<T> {

    private static final boolean similaritySkew = false;
    private final IShufflingIdGenerator gen;
    protected T lastSimilar;

    public ShufflingComparator(IShufflingIdGenerator gen) {
        this.gen = gen;
    }

    ShufflingComparator() {
        this(new ShufflingIdGenerator());
    }

    protected abstract boolean similarityTest(T o1, T o2);

    @Override
    public int compare(T o1, T o2) {
        boolean similarityTest = similarityTest(o1, o2);

        if (similaritySkew) {
            if (lastSimilar != null && similarityTest && !similarityTest(o1, lastSimilar)) {
                lastSimilar = o1;
                similarityTest = false;
            }
        }

        gen.assureIds(o1, o2, similarityTest);

        if (similarityTest) {
            if (similarityTest) {
                lastSimilar = o1;
            }
        }
        /*
         * final int difference = o2.getShuffleId()-o1.getShuffleId();
         * assert(difference!=0||o1==o2): "Shuffling id's should be different";
         */
        return o1.getShuffleId().compareTo(o2.getShuffleId());
    }

    @Override
    public String toString() {
        return getClass().getSimpleName()+"{" + "gen=" + gen + '}';
    }
}
